﻿
using System;
using System.Threading.Tasks;

namespace Magic.Redis;


/// <summary>
/// 提供Redis客户端操作类（基于CsRedisCore实现）
/// </summary>
public sealed partial class CsRedisCoreClient : IRedisClient
{
	/// <summary>
	/// 返回指定的 key 的string类型数据，key不存在时，将返回默认值
	/// </summary>
	/// <typeparam name="T">返回的类型</typeparam>
	/// <param name="key"></param>
	/// <returns>返回获取到的数据<see cref="StringGet{T}(string)"/></returns>
	public T StringGet<T>(string key)
	{
		if (!KeyExists(key))
		{
			return default;
		}
		try
		{
			return RedisHelper.Get<T>(key);
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on StringGet.key={key}", ex);
			return default;
		}
	}

	/// <summary>
	/// 异步的返回指定的redis key的string类型数据，key不存在时，将返回默认值
	/// </summary>
	/// <typeparam name="T">返回的类型</typeparam>
	/// <param name="key"></param>
	/// <returns></returns>
	public async Task<T> StringGetAsync<T>(string key)
	{
		if (!await KeyExistsAsync(key))
		{
			return default;
		}
		try
		{
			return await RedisHelper.GetAsync<T>(key);
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on StringGetAsync.key={key}", ex);
			return default;
		}
	}

	/// <summary>
	/// 返回指定的redis key对应的string数据的长度，key不存在将返回默认值
	/// </summary>
	/// <param name="key"></param>
	/// <returns>返回<see cref="long"/>类型的数据的长度</returns>
	public long StringLength(string key)
	{
		if (!KeyExists(key))
		{
			return default;
		}
		try
		{
			return RedisHelper.StrLen(key);
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on StringLength.key={key}", ex);
			return default;
		}
	}

	/// <summary>
	/// 异步的返回指定的redis key对应的string数据的长度，key不存在将返回默认值
	/// </summary>
	/// <param name="key"></param>
	/// <returns>返回<see cref="long"/>类型的数据的长度</returns>
	public async Task<long> StringLengthAsync(string key)
	{
		if (!await KeyExistsAsync(key))
		{
			return default;
		}
		try
		{
			return await RedisHelper.StrLenAsync(key);
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on StringLengthAsync.key={key}", ex);
			return default;
		}
	}

	/// <summary>
	/// 设置string类型的redis数据
	/// </summary>
	/// <typeparam name="T">传入的value的类型</typeparam>
	/// <param name="key"></param>
	/// <param name="value"></param>
	/// <param name="expiry">（滑动过期）过期时间间隔<see cref="TimeSpan"/></param>
	/// <returns></returns>
	public bool StringSet<T>(string key, T value, TimeSpan? expiry = null)
	{
		try
		{
			return RedisHelper.Set(key, value, expiry.Value);
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on StringSet.key={key}&value={Serialize(value)}", ex);
			return false;
		}
	}

	/// <summary>
	/// 异步的设置string类型的redis数据
	/// </summary>
	/// <typeparam name="T">传入的value的类型</typeparam>
	/// <param name="key"></param>
	/// <param name="value"></param>
	/// <param name="expiry">（滑动过期）过期时间间隔<see cref="TimeSpan"/></param>
	/// <returns></returns>
	public async Task<bool> StringSetAsync<T>(string key, T value, TimeSpan? expiry = null)
	{
		try
		{
			return await RedisHelper.SetAsync(key, value, expiry.Value);
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on StringSetAsync.key={key}&value={Serialize(value)}", ex);
			return false;
		}
	}

	/// <summary>
	/// 计数器，计数并返回当前数值
	/// </summary>
	/// <param name="key"></param>
	/// <param name="value"></param>
	/// <returns></returns>
	public long StringIncrement(string key, long value = 1L)
	{
		try
		{
			return RedisHelper.IncrBy(key, value);
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on StringIncrement.key={key}&value={value}", ex);
			return default;
		}
	}

	/// <summary>
	/// 异步的计数器，计数并返回当前数值
	/// </summary>
	/// <param name="key"></param>
	/// <param name="value"></param>
	/// <returns></returns>
	public async Task<long> StringIncrementAsync(string key, long value = 1L)
	{
		try
		{
			return await RedisHelper.IncrByAsync(key, value);
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on StringIncrementAsync.key={key}&value={value}", ex);
			return default;
		}
	}
}
